<template>
  <div>
  <el-card>
    <el-form :label-position="labelPosition" label-width="80px" :model="content" :rules="ArticleRules" ref="content">
      <el-form-item label="文章标题" style="width: 300px" prop="title">
        <el-input v-model="content.title" placeholder="请输入文章标题"></el-input>
      </el-form-item>
      <el-form-item label="文章摘要" prop="brief">
        <el-input
            type="textarea"
            :rows="5"
            placeholder="请输入文章摘要"
            v-model="content.brief">
        </el-input>
      </el-form-item>
      <el-form-item label="文章内容" prop="content">
        <el-input
            type="textarea"
            :rows="25"
            placeholder="请输入文章内容"
            v-model="content.content">
        </el-input>
      </el-form-item>
      <el-form-item label="参考资料" prop="reference">
        <el-input
            type="textarea"
            :rows="5"
            placeholder="请输入参考文献"
            v-model="content.reference">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitInfoForm('content')">立即发布</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  </div>
</template>
<script>
import BaseUrl from "@/http/BaseUrl";
import axios from "axios";
import qs from 'qs'
import TokenAxios from "@/http/TokenAxios";
import SimpleAxios from "@/http/SimpleAxios";

export default {
  data() {
    return {
      loginInfo:[],
      labelPosition: 'right',
      content: {title: '', brief: '', content: '', reference: '',
      },
          ArticleRules: {
        title: [{required: true, message: '请输入文章标题', trigger: 'blur'}],
        brief: [{required: true, message: '请输入文章摘要', trigger: 'blur'}],
        content: [{required: true, message: '请输入文章内容', trigger: 'blur'}],
        reference: [{required: true, message: '请输入文章参考文献', trigger: 'blur'}]
      }
     
    };
  },
  methods: {
    loadCurrentLoginInfo() {
      let loginInfoString=localStorage.getItem('loginInfo')

      this.loginInfo = JSON.parse(loginInfoString);
      if (!this.loginInfo) {
        this.$router.push('/login/user');
        return;
      }else if (this.loginInfo.role!==3&&this.loginInfo.role!==4){
        this.$message.error('无权访问');
        this.$router.push('/home');
        return;
      }
    },
    submitInfoForm(formName) {
        this.$refs[formName].validate((valid) => {
            if (valid) {
                let url = BaseUrl.POST_DOCTOR + '/article/post';
                // const formData = qs.stringify(this.formLabelArticle);
                console.log('url=' + url);
                // console.log('formData: ' + formData);
                TokenAxios.post(url, this.content, {
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                }).then((response) => {
                    let jsonResult = response.data;
                    if (jsonResult.state === 20000) {
                        this.$message({
                            message: '发布成功', type: 'success'
                        });
                        location.href="/encyclopedia"
                    } else {
                        this.$message.error(response.data.message);
                    }
                });
            }else{
                console.log("输入错误");
                return false;
            }
        });


    }
  },
  created() {

  },
  mounted() {
    this.loadCurrentLoginInfo();
  }
}
</script>

